namespace Plugins.Engine.Internal
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using Plugins.Core;
    using Plugins.Core.Interfaces;
    using Plugins.Library;
    using Plugins.Interfaces;

    [Serializable]
    internal class PluginObjectActivator<T> : PluginObjectActivator, IPluginObjectActivatorInternal, IPluginAssemblyActivatorInternal, IPluginObjectActivator<T>, IPluginObjectActivator
    {
        private bool _loaded;
        private T _object;

        internal PluginObjectActivator(Type type, IPluginContextInternal context, IPluginObjectActivatorInternal instance)
            : base(type, context, instance)
        {
        }

        private T GetInstance()
        {
            if (!this._loaded)
            {
                this._object = (T)this.GetObject();
                this._loaded = true;
            }
            return this._object;
        }

        [SuppressMessage("Microsoft.Design", "CA1031")]
        public void ProcessObject(PluginObjectAction action)
        {
            try
            {
                if (InternalContext.IsLoaded)
                {
                    if (action != null)
                    {
                        action(this.GetInstance());
                    }
                }
            }
            catch (Exception ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
        }

        [SuppressMessage("Microsoft.Design", "CA1031")]
        public bool ProcessObject(PluginObjectPredicate match)
        {
            try
            {
                if (InternalContext.IsLoaded)
                {
                    if (match != null)
                    {
                        return match(this.GetInstance());
                    }
                }
            }
            catch (Exception ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            return false;
        }

        [SuppressMessage("Microsoft.Design", "CA1031")]
        public void ProcessObject(Action<T> action)
        {
            try
            {
                if (InternalContext.IsLoaded)
                {
                    if (action != null)
                    {
                        action(this.GetInstance());
                    }
                }
            }
            catch (Exception ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
        }

        [SuppressMessage("Microsoft.Design", "CA1031")]
        public bool ProcessObject(Predicate<T> match)
        {
            try
            {
                if (InternalContext.IsLoaded)
                {
                    if (match != null)
                    {
                        return match(this.GetInstance());
                    }
                }
            }
            catch (Exception ex)
            {
                PluginLog.WriteErrorSync(ex);
            }
            return false;
        }

        void IPluginObjectActivatorInternal.Activate()
        {
            InternalActivator.Activate();
        }

        void IPluginObjectActivatorInternal.Deactivate()
        {
            InternalActivator.Deactivate();
        }

        bool IPluginObjectActivatorInternal.IsActive
        {
            get
            {
                return InternalActivator.IsActive;
            }
        }
    }
}

